<?php

function writelogs($filename, $msg)
{
    $fd = fopen($filename, "a");
    $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
    fwrite($fd, $str . "\n");
    fclose($fd);
}

$xmlDoc = new DOMDocument();
$xmlDoc->load("links.xml");
$logfilename = "process_live.log";
$double_quotes = '"';

$x = $xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q = $_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q) > 0) {
    $hint = "";
    for ($i = 0; $i < ($x->length); $i++) {
        $y = $x->item($i)->getElementsByTagName('name');
        $z = $x->item($i)->getElementsByTagName('area');
        if ($y->item(0)->nodeType == 1) {
            //find a link matching the search text
            if (stristr($y->item(0)->childNodes->item(0)->nodeValue, $q)) {
                if ($hint == "") {
                    $hint = "<table>" . "<tr>" . "<td>" . $y->item(0)->childNodes->item(0)->nodeValue . "</td>" . "<td>" . $z->item(0)->childNodes->item(0)->nodeValue . "</td>" . "<td>" . "<a href=$double_quotes#$double_quotes>Book Appointment" . "</a>" . "</td>" ."</tr>";
                } else {
                    $hint = "<td>" . $hint . "</td>" . "<td>" . $y->item(0)->childNodes->item(0)->nodeValue . "</td>" . "<td>" . $z->item(0)->childNodes->item(0)->nodeValue . "</td>" . "<td>" . "<a href=$double_quotes#$double_quotes>Book Appointment" . "</a>" . "</td>" . "<table>";
                }
            }
        }
    }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "") {
    $response = "no suggestion";
} else {
    $response = $hint;
}

//output the response
echo $response;
?>